package com.springboot.blog.interceptor;

import org.apache.shiro.web.filter.authc.UserFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class ShiroLoginFilter extends UserFilter {

    @Override
    protected void redirectToLogin(ServletRequest request, ServletResponse response) throws IOException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        String requestedWith = httpServletRequest.getHeader("X-Requested-With");//判断ajax请求
        if (requestedWith == null || !requestedWith.equalsIgnoreCase("XMLHttpRequest")) {
            super.redirectToLogin(request, response);
        }
    }
}
